package com.mes.cloud.util;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页返回值类
 *
 * @version 1.0
 * @date 创建时间：2015-5-28 下午2:00:33
 * @explain 分页工具
 */
@ApiModel
public class CommPager<T> implements Serializable {
    private static final long serialVersionUID = -224189960966026897L;

    @ApiModelProperty(value = "总条数")
    private long totalRecord;

    @ApiModelProperty(value = "总页数")
    private int totalPage;

    @ApiModelProperty(value = "当前页码")
    private int pageIndex;

    @ApiModelProperty(value = "当前页码记录数")
    private int currenRecords;

    @ApiModelProperty(value = "每页容量")
    private int pageSize;

    @ApiModelProperty(value = "数据记录")
    private List<T> records;

    /**
     * 构造函数
     */
    public CommPager() {
        this.records = new ArrayList<T>();
        this.totalRecord = -1; // 总条数
        this.pageSize = -1; // 每页容量
        this.totalPage = -1; // 总页数
        this.pageIndex = -1; // 当前页码
    }

    /**
     * 构造函数
     *
     * @param list 分页数据
     */
    public CommPager(List<T> list) {
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            this.records = page.getResult();
            this.currenRecords = page.size();
            this.pageSize = page.getPageSize();
            this.pageIndex = page.getPageNum();
            if (page.isCount()) {
                this.totalRecord = page.getTotal();
                this.totalPage = page.getPages();
            } else {
                this.totalRecord = -1;
                this.totalPage = -1;
            }
        }
    }

    /**
     * 构造函数
     *
     * @param page
     * @param list 分页数据
     */
    public CommPager(Page page, List<T> list) {
        this.records = list;
        this.currenRecords = page.size();
        this.pageSize = page.getPageSize();
        this.pageIndex = page.getPageNum();
        this.totalRecord = page.getTotal();
        this.totalPage = page.getPages();
    }

    public void setPage(Page page){
        this.currenRecords = page.size();
        this.pageSize = page.getPageSize();
        this.pageIndex = page.getPageNum();
        this.totalRecord = page.getTotal();
        this.totalPage = page.getPages();
    }

    /**
     * @param total     总条数
     * @param pageSize  每页条数
     * @param pageIndex 当前页码
     * @param list      分页数据
     */
    public CommPager(Integer total, Integer pageSize, Integer pageIndex, List<T> list) {
        this.records = list;
        this.currenRecords = list.size();
        this.pageSize = pageSize;
        this.pageIndex = pageIndex;
        this.totalRecord = total;
        this.totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
    }


    /**
     * 总记录数
     *
     * @return
     */
    public long getTotalRecord() {
        return totalRecord;
    }

    /**
     * 总记录数
     *
     * @param totalRecord
     */
    public void setTotalRecord(long totalRecord) {
        this.totalRecord = totalRecord;
    }

    /**
     * 总页数
     *
     * @return
     */
    public int getTotalPage() {
        return totalPage;
    }

    /**
     * 总页数
     *
     * @param totalPage
     */
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * 当前页码
     *
     * @return
     */
    public int getPageIndex() {
        return pageIndex;
    }

    /**
     * 当前页码
     *
     * @param pageIndex
     */
    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    /**
     * 每页数据量
     *
     * @return
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 每页数据量
     *
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 记录
     *
     * @return
     */
    public List<T> getRecords() {
        return records;
    }

    /**
     * 记录
     *
     * @param records
     */
    public void setRecords(List<T> records) {
        this.records = records;
    }

    /**
     * 当前页码记录数
     *
     * @return
     */
    public int getCurrenRecords() {
        return currenRecords;
    }

    /**
     * 当前页码记录数
     *
     * @param currenRecords
     */
    public void setCurrenRecords(int currenRecords) {
        this.currenRecords = currenRecords;
    }

}